package com.itboye.sunsun.adapter;

import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.GoodBean;
import com.itboye.sunsun.shop.ui.GoodDetailActivity;
import com.itboye.sunsun.shop.ui.ShopActivity;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.XImageLoader;

import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ShopAdapter extends BaseAdapter {

	private List<GoodBean>list;
	private Activity activity;
	public ShopAdapter(List<GoodBean>list, Activity activity) {
		this.list=list;
		this.activity=activity;
	}

	@Override
	public int getCount() {
		return (list.size()+1)/2;
	}

	
	
	/**
	 * 无数据，暂时写成下面这样
	 */
	@Override
	public String getItem(int position) {
		return list.get(position).toString();
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			convertView=LayoutInflater.from(App.ctx).inflate(R.layout.item_goods, null);
			holder=new ViewHolder();
			holder.leftImageView=(ImageView) convertView.findViewById(R.id.leftImageView);
			holder.rightImageView=(ImageView) convertView.findViewById(R.id.rightImageView);
			convertView.setTag(holder);
			
			holder.leftImageView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(activity, GoodDetailActivity.class);
					intent.putExtra("what", (String)v.getTag());
					activity.startActivity(intent);
				}
			});
			
			holder.rightImageView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(activity, GoodDetailActivity.class);
					intent.putExtra("what", (String)v.getTag());
					activity.startActivity(intent);
				}
			});
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		
		holder.leftImageView.setTag(getItem(position*2));
		holder.rightImageView.setTag(getItem(position*2+1));
		
		XImageLoader.load(list.get(position*2).getUrl(), holder.leftImageView);
		XImageLoader.load(list.get(position*2+1).getUrl(), holder.rightImageView);
		
		return convertView;
	}

	class ViewHolder {
		ImageView  leftImageView ;
		ImageView rightImageView;
		
	}
}
